package com.mpyy.handler.systemSetting;

import com.alibaba.fastjson.JSON;
import com.mpyy.dao.ToolInter;
import com.mpyy.pojo.CustomerInfo;
import com.mpyy.pojo.MenmberInfo;
import com.mpyy.pojo.Paging;
import com.mpyy.service.systemSetting.MemberServiceImpl;
import com.mpyy.tool.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @PackageName:com.mpyy.handler.systemSetting
 * @ClassName:Member
 * @Date:2022-03-07:9:36
 * @Author: Tang Neng
 */
@Controller
public class MemberHandler {
    @Autowired
    private MemberServiceImpl memberService;
    @Autowired
    private ToolInter toolInter;

    /**
     * 客户信息的查询
     * @param custName
     * @return
     */
    @RequestMapping(value = "/searchMemberInfo" , method = RequestMethod.GET)
    @ResponseBody
    public String searchMemberInfo(String custName, Paging paging){
        System.out.println("客户"+paging);
        //调用客户管理里面查询客户信息的方法用一个集合对数据进行储存
        List<CustomerInfo> customerInfos = memberService.searchMemberInfo(custName , paging);
        //创建一个HashMap对象
        HashMap<Object, Object> customerMap = new HashMap<>();
        //对JSON格式进行拼接
        customerMap.put("code",0);
        customerMap.put("msg","");
        customerMap.put("count",memberService.searchMemberInfo(custName ,null).size());
        customerMap.put("data",customerInfos);
        //装换成JSON格式的字符串，返回到页面
        return JSON.toJSONString(customerMap);
    }
    @Autowired
    private ClientHandler clientHandler;
    /**
     * 客户信息的添加
     * @return
     */
    @RequestMapping(value = "/updateMemberInfoAdd" , method = RequestMethod.POST)
    @ResponseBody
    public int updateMemberInfoAdd(CustomerInfo customerInfo){
        //调用自动生成id方法
        String s = new Tool().aotomaticId(customerInfo.getCustId(), toolInter);
        //给id属性赋值
        customerInfo.setCustId(s);
        SimpleDateFormat dateFormat = new SimpleDateFormat(" yyyy-MM-dd ");
        //判断会员编号是否为空，不为空自动新增一个会员
        if(null != customerInfo.getCustMenmber() && customerInfo.getCustMenmber() != ""){
            MenmberInfo menmber = new MenmberInfo();
            menmber.setMenId(customerInfo.getCustMenmber());//会员ID
            menmber.setMenName(customerInfo.getCustName());//会员名称
            menmber.setMenTypeId("men001");//会员级别
            menmber.setMenStatus("1");//会员状态
            menmber.setMenStartDate(dateFormat.format( new Date() ));//加入日期
            menmber.setMenPass("123456");//会员密码
            menmber.setMenPhone(customerInfo.getCustPhone());//会员电话
            menmber.setMenPointUpgrade("1");//按积分升级
            menmber.setMenPreferential("不打折");//优惠方式
            int i = clientHandler.updateClientInfoAdd(menmber);
        }
        int row = memberService.updataMemberInfoAdd(customerInfo);
        return row;
    }

    /**
     * 客户信息的删除
     * @param custId
     * @return
     */
    @RequestMapping(value = "/updateMemberInfoDelect" , method = RequestMethod.GET)
    @ResponseBody
    public int updateMemberInfoDelect(String custId){
        return memberService.updataMemberInfoDelect(custId);
    }

    /**
     * 客户信息的修改
     * @param customerInfo
     * @return
     */
    @RequestMapping(value = "/updateMemberInfo" , method = RequestMethod.GET)
    @ResponseBody
    public int updateMemberInfo(CustomerInfo customerInfo){
        return memberService.updataMemberInfo(customerInfo);
    }
}
